package cn.jtcoding.item.web.controller;

import cn.jtcoding.common.framework.pagehelper.PagedGridResult;
import cn.jtcoding.item.api.ItemService;
import cn.jtcoding.item.pojo.dataobject.ItemsDO;
import cn.jtcoding.item.pojo.dataobject.ItemsSpecDO;
import cn.jtcoding.item.pojo.vo.ItemCommentLevelCountVO;
import cn.jtcoding.item.pojo.vo.ItemCommentVO;
import cn.jtcoding.item.pojo.vo.ItemInfoVO;
import cn.jtcoding.item.pojo.vo.ItemSearchVO;
import cn.jtcoding.common.framework.vo.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author jason.tang
 * @date 2021/3/10 12:21
 * @desc
 */

@RestController
@RequestMapping("/items")
public class ItemController {

    @Autowired
    private ItemService itemService;

    @GetMapping("/info/{itemId}")
    public ApiResult<ItemInfoVO> getItemInfo(@PathVariable String itemId) {
        return ApiResult.success(itemService.getItemInfo(itemId));
    }

    @GetMapping("/commentLevel")
    public ApiResult<ItemCommentLevelCountVO> getItemComments(@RequestParam String itemId) {
        return ApiResult.success(itemService.getItemCommentCount(itemId));
    }

    @GetMapping("/comments")
    public ApiResult<PagedGridResult<ItemCommentVO>> getItemComments(@RequestParam String itemId, @RequestParam Integer level,
                                                                     @RequestParam Integer page, @RequestParam Integer pageSize) {
        return ApiResult.success(itemService.getItemComments(itemId, level, page, pageSize));
    }

    @GetMapping("/search")
    public ApiResult<PagedGridResult<ItemSearchVO>> searchItems(@RequestParam String keywords, @RequestParam String sort,
                                                                @RequestParam Integer page, @RequestParam Integer pageSize) {
        return ApiResult.success(itemService.searchItems(keywords, sort, page, pageSize));
    }

    @GetMapping("/refresh")
    public ApiResult<?> refresh(
            @RequestParam String itemSpecIds) {
        if (StringUtils.isBlank(itemSpecIds)) {
            return ApiResult.success();
        }
        // 刷新购物车
        return ApiResult.success();
    }

    @GetMapping("/spec/{itemSpecId}")
    public ApiResult<ItemsSpecDO> queryItemSpecById(@PathVariable String itemSpecId) {
        return ApiResult.success(itemService.queryItemSpecById(itemSpecId));
    }

    @GetMapping("/{itemId}")
    public ApiResult<ItemsDO> queryItemById(@PathVariable String itemId) {
        return ApiResult.success(itemService.queryItemById(itemId));
    }

    @GetMapping("/img/{itemId}/main")
    public ApiResult<String> queryItemMainImgById(@PathVariable String itemId) {
        return ApiResult.success(itemService.queryItemMainImgById(itemId));
    }

    @GetMapping("/spec/{itemSpecId}/dec_stock")
    public ApiResult<?> decreaseItemSpecStock(@PathVariable String itemSpecId, @RequestParam Integer buyCounts) {
        itemService.decreaseItemSpecStock(itemSpecId, buyCounts);
        return ApiResult.success();
    }


}
